package com.example.testplatform.controller;

import com.example.testplatform.common.LogUtils;
import com.example.testplatform.common.Result;
import com.example.testplatform.entity.User;
import com.example.testplatform.service.UserService;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户管理控制器
 * 提供用户管理的CRUD操作
 */
@RestController
@RequestMapping("/api/system/users")
public class UserManageController {
    
    private static final Logger logger = LogUtils.getLogger(UserManageController.class);
    
    @Autowired
    private UserService userService;
    
    /**
     * 获取用户列表
     * @param page 页码
     * @param pageSize 每页数量
     * @param username 用户名搜索
     * @param role 角色搜索
     * @param status 状态搜索
     * @return 用户列表
     */
    @GetMapping
    public Result<Map<String, Object>> getUserList(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String username,
            @RequestParam(required = false) String role,
            @RequestParam(required = false) String status) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_GET_LIST_ATTEMPT", "system", "获取用户列表尝试: 页码=" + page + ", 每页数量=" + pageSize + ", 搜索条件=[用户名=" + username + ", 角色=" + role + ", 状态=" + status + "]");
        LogUtils.debug(logger, () -> "开始获取用户列表，页码=" + page + ", 每页数量=" + pageSize + ", 搜索条件=[用户名=" + username + ", 角色=" + role + ", 状态=" + status + "]");
        
        try {
            // 构建查询参数
            Map<String, Object> params = new HashMap<>();
            params.put("username", username);
            params.put("role", role);
            params.put("status", status);
            
            LogUtils.debug(logger, () -> "构建查询参数完成: " + params);
            
            // 使用服务层提供的完整分页查询方法
            Map<String, Object> result = userService.getUserList(page, pageSize, params);
            
            // 提取结果中的总数
            int total = result != null && result.containsKey("total") ? Integer.parseInt(result.get("total").toString()) : 0;
            
            LogUtils.info(logger, () -> "获取用户列表成功，页码=" + page + ", 每页数量=" + pageSize + ", 总记录数=" + total);
            LogUtils.logBusiness(logger, "USER_GET_LIST_SUCCESS", "system", "获取用户列表成功，页码=" + page + ", 每页数量=" + pageSize + ", 总记录数=" + total);
            LogUtils.logPerformance(logger, "getUserList", startTime, System.currentTimeMillis());
            
            return Result.success(result);
        } catch (Exception e) {
            LogUtils.error(logger, "获取用户列表失败: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_GET_LIST_FAILED", "system", "获取用户列表失败: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "getUserList", startTime, System.currentTimeMillis());
            return Result.fail("获取用户列表失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户详情
     * @param id 用户ID
     * @return 用户详情
     */
    @GetMapping("/{id}")
    public Result<User> getUserById(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_GET_DETAIL_ATTEMPT", "system", "获取用户详情尝试: 用户ID=" + id);
        LogUtils.debug(logger, () -> "开始获取用户详情: 用户ID=" + id);
        
        try {
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "获取用户详情失败: 参数校验失败，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_GET_DETAIL_FAILED", "system", "获取用户详情失败: 参数校验失败，用户ID=" + id);
                return Result.fail("用户ID不能为空或无效");
            }
            
            User user = userService.getById(id);
            if (user != null) {
                // 移除密码等敏感信息
                user.setPassword(null);
                
                LogUtils.info(logger, () -> "获取用户详情成功: 用户ID=" + id + ", 用户名=" + user.getUsername() + ", 角色=" + user.getRole() + ", 状态=" + user.getStatus());
                LogUtils.logBusiness(logger, "USER_GET_DETAIL_SUCCESS", "system", "获取用户详情成功: 用户ID=" + id + ", 用户名=" + user.getUsername());
                LogUtils.logPerformance(logger, "getUserById", startTime, System.currentTimeMillis());
                
                return Result.success(user);
            } else {
                LogUtils.warn(logger, "获取用户详情失败: 用户不存在，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_GET_DETAIL_FAILED", "system", "获取用户详情失败: 用户不存在，用户ID=" + id);
                LogUtils.logPerformance(logger, "getUserById", startTime, System.currentTimeMillis());
                
                return Result.fail("用户不存在");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "获取用户详情失败: 用户ID=" + id + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_GET_DETAIL_FAILED", "system", "获取用户详情失败: 用户ID=" + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "getUserById", startTime, System.currentTimeMillis());
            
            return Result.fail("获取用户详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 创建用户
     * @param user 用户信息
     * @return 创建结果
     */
    @PostMapping
    public Result<String> createUser(@RequestBody User user) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_CREATE_ATTEMPT", "system", "创建用户尝试");
        LogUtils.logSecurityEvent(logger, "USER_CREATE_REQUEST", "接收到创建用户请求", "请求包含的用户名: " + (user != null ? user.getUsername() : "空"));
        LogUtils.debug(logger, () -> "开始创建用户: " + (user != null ? user.toString() : "空用户"));
        
        try {
            // 验证参数
            if (user == null) {
                LogUtils.warn(logger, "创建用户失败: 用户信息为空");
                LogUtils.logBusiness(logger, "USER_CREATE_FAILED", "system", "创建用户失败: 用户信息为空");
                return Result.fail("用户信息不能为空");
            }
            
            if (user.getUsername() == null || user.getPassword() == null) {
                LogUtils.warn(logger, "创建用户失败: 用户名或密码不能为空，用户名={}", user.getUsername());
                LogUtils.logBusiness(logger, "USER_CREATE_FAILED", "system", "创建用户失败: 用户名或密码不能为空");
                return Result.fail("用户名或密码不能为空");
            }
            
            // 检查用户名是否已存在
            User existingUser = userService.getByUsername(user.getUsername());
            if (existingUser != null) {
                LogUtils.warn(logger, "创建用户失败: 用户名已存在，用户名={}", user.getUsername());
                LogUtils.logBusiness(logger, "USER_CREATE_FAILED", "system", "创建用户失败: 用户名已存在，用户名=" + user.getUsername());
                return Result.fail("用户名已存在");
            }
            
            // 设置默认值
            if (user.getStatus() == null) {
                user.setStatus(1); // 默认启用
                LogUtils.debug(logger, () -> "用户状态未设置，默认设置为启用状态");
            }
            if (user.getRole() == null) {
                user.setRole("TESTER"); // 默认测试人员角色
                LogUtils.debug(logger, () -> "用户角色未设置，默认设置为TESTER角色");
            }
            
            // 设置创建时间和更新时间
            user.setCreateTime(LocalDateTime.now());
            user.setUpdateTime(LocalDateTime.now());
            LogUtils.debug(logger, () -> "设置用户创建时间和更新时间: createTime=" + user.getCreateTime() + ", updateTime=" + user.getUpdateTime());
            
            // 添加调试信息，验证时间字段确实被设置
            LogUtils.info(logger, () -> "【用户管理】创建用户前的时间字段验证: createTime=" + user.getCreateTime() + ", updateTime=" + user.getUpdateTime());
            LogUtils.info(logger, () -> "【用户管理】创建用户前的完整用户对象: " + user.toString());
            
            LogUtils.debug(logger, () -> "参数校验通过，开始保存用户信息: " + user.getUsername() + "，角色: " + user.getRole() + "，状态: " + user.getStatus());
            
            boolean success = userService.save(user);
            if (success) {
                LogUtils.info(logger, () -> "创建用户成功: 用户名=" + user.getUsername() + ", 用户ID=" + user.getId() + ", 角色=" + user.getRole() + ", 状态=" + user.getStatus());
                LogUtils.logBusiness(logger, "USER_CREATE_SUCCESS", "system", "创建用户成功: 用户名=" + user.getUsername() + ", 用户ID=" + user.getId());
                LogUtils.logSecurityEvent(logger, "USER_CREATED", "用户创建成功", "用户名: " + user.getUsername() + ", 用户ID: " + user.getId());
                LogUtils.logPerformance(logger, "createUser", startTime, System.currentTimeMillis());
                
                return Result.success("创建用户成功");
            } else {
                LogUtils.warn(logger, "创建用户失败: 用户名={}", user.getUsername());
                LogUtils.logBusiness(logger, "USER_CREATE_FAILED", "system", "创建用户失败: 用户名=" + user.getUsername());
                LogUtils.logPerformance(logger, "createUser", startTime, System.currentTimeMillis());
                
                return Result.fail("创建用户失败");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "创建用户失败: " + (user != null ? user.getUsername() : "空用户") + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_CREATE_FAILED", "system", "创建用户失败: " + (user != null ? user.getUsername() : "空用户") + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "createUser", startTime, System.currentTimeMillis());
            
            return Result.fail("创建用户失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新用户
     * @param id 用户ID
     * @param user 用户信息
     * @return 更新结果
     */
    @PutMapping("/{id}")
    public Result<String> updateUser(@PathVariable Long id, @RequestBody User user) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_UPDATE_ATTEMPT", "system", "更新用户尝试: 用户ID=" + id);
        LogUtils.logSecurityEvent(logger, "USER_UPDATE_REQUEST", "接收到更新用户请求", "用户ID: " + id);
        LogUtils.debug(logger, () -> "开始更新用户: 用户ID=" + id + ", 用户信息=" + (user != null ? user.toString() : "空用户"));
        
        try {
            // 验证参数
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "更新用户失败: 用户ID不能为空或无效");
                LogUtils.logBusiness(logger, "USER_UPDATE_FAILED", "system", "更新用户失败: 用户ID不能为空或无效");
                return Result.fail("用户ID不能为空或无效");
            }
            
            if (user == null) {
                LogUtils.warn(logger, "更新用户失败: 用户信息为空");
                LogUtils.logBusiness(logger, "USER_UPDATE_FAILED", "system", "更新用户失败: 用户信息为空");
                return Result.fail("用户信息不能为空");
            }
            
            // 检查用户是否存在
            User existingUser = userService.getById(id);
            if (existingUser == null) {
                LogUtils.warn(logger, "更新用户失败: 用户不存在，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_UPDATE_FAILED", "system", "更新用户失败: 用户不存在，用户ID=" + id);
                return Result.fail("用户不存在");
            }
            
            LogUtils.debug(logger, () -> "用户存在，用户名=" + existingUser.getUsername() + ", 开始准备更新操作");
            
            // 设置ID
            user.setId(id);
            
            // 如果不修改密码，保持原密码
            if (user.getPassword() == null || user.getPassword().isEmpty()) {
                user.setPassword(existingUser.getPassword());
                LogUtils.debug(logger, () -> "密码未修改，保持原有密码");
            } else {
                LogUtils.debug(logger, () -> "密码已修改，将更新为新密码");
            }
            
            LogUtils.debug(logger, () -> "参数校验通过，开始执行更新操作: 用户ID=" + id + ", 用户名=" + existingUser.getUsername());
            
            boolean success = userService.updateById(user);
            if (success) {
                LogUtils.info(logger, () -> "更新用户成功: 用户ID=" + id + ", 用户名=" + existingUser.getUsername() + ", 角色=" + (user.getRole() != null ? user.getRole() : existingUser.getRole()));
                LogUtils.logBusiness(logger, "USER_UPDATE_SUCCESS", "system", "更新用户成功: 用户ID=" + id + ", 用户名=" + existingUser.getUsername());
                LogUtils.logSecurityEvent(logger, "USER_UPDATED", "用户信息更新成功", "用户ID: " + id + ", 用户名: " + existingUser.getUsername());
                LogUtils.logPerformance(logger, "updateUser", startTime, System.currentTimeMillis());
                
                return Result.success("更新用户成功");
            } else {
                LogUtils.warn(logger, "更新用户失败: 用户ID={}, 用户名={}", id, existingUser.getUsername());
                LogUtils.logBusiness(logger, "USER_UPDATE_FAILED", "system", "更新用户失败: 用户ID=" + id + ", 用户名=" + existingUser.getUsername());
                LogUtils.logPerformance(logger, "updateUser", startTime, System.currentTimeMillis());
                
                return Result.fail("更新用户失败");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "更新用户失败: 用户ID=" + id + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_UPDATE_FAILED", "system", "更新用户失败: 用户ID=" + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "updateUser", startTime, System.currentTimeMillis());
            
            return Result.fail("更新用户失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除用户
     * @param id 用户ID
     * @return 删除结果
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteUser(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_DELETE_ATTEMPT", "system", "删除用户尝试: 用户ID=" + id);
        LogUtils.logSecurityEvent(logger, "USER_DELETE_REQUEST", "接收到删除用户请求", "用户ID: " + id);
        LogUtils.debug(logger, () -> "开始删除用户: 用户ID=" + id);
        
        try {
            // 验证参数
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "删除用户失败: 用户ID不能为空或无效");
                LogUtils.logBusiness(logger, "USER_DELETE_FAILED", "system", "删除用户失败: 用户ID不能为空或无效");
                return Result.fail("用户ID不能为空或无效");
            }
            
            // 检查用户是否存在
            User user = userService.getById(id);
            if (user == null) {
                LogUtils.warn(logger, "删除用户失败: 用户不存在，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_DELETE_FAILED", "system", "删除用户失败: 用户不存在，用户ID=" + id);
                return Result.fail("用户不存在");
            }
            
            // 不允许删除管理员用户
            if ("ADMIN".equals(user.getRole())) {
                LogUtils.warn(logger, "删除用户失败: 管理员用户不允许删除，用户名={}", user.getUsername());
                LogUtils.logBusiness(logger, "USER_DELETE_FAILED", "system", "删除用户失败: 管理员用户不允许删除，用户名=" + user.getUsername());
                LogUtils.logSecurityEvent(logger, "USER_DELETE_ATTEMPTED_ADMIN", "尝试删除管理员用户被拒绝", "用户ID: " + id + ", 用户名: " + user.getUsername());
                return Result.fail("管理员用户不允许删除");
            }
            
            LogUtils.debug(logger, () -> "参数校验通过，开始执行删除操作: 用户ID=" + id + ", 用户名=" + user.getUsername() + ", 角色=" + user.getRole());
            
            boolean success = userService.removeById(id);
            if (success) {
                LogUtils.info(logger, () -> "删除用户成功: 用户ID=" + id + ", 用户名=" + user.getUsername() + ", 角色=" + user.getRole());
                LogUtils.logBusiness(logger, "USER_DELETE_SUCCESS", "system", "删除用户成功: 用户ID=" + id + ", 用户名=" + user.getUsername());
                LogUtils.logSecurityEvent(logger, "USER_DELETED", "用户删除成功", "用户ID: " + id + ", 用户名: " + user.getUsername());
                LogUtils.logPerformance(logger, "deleteUser", startTime, System.currentTimeMillis());
                
                return Result.success("删除用户成功");
            } else {
                LogUtils.warn(logger, "删除用户失败: 用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_DELETE_FAILED", "system", "删除用户失败: 用户ID=" + id);
                LogUtils.logPerformance(logger, "deleteUser", startTime, System.currentTimeMillis());
                
                return Result.fail("删除用户失败");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "删除用户失败: 用户ID=" + id + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_DELETE_FAILED", "system", "删除用户失败: 用户ID=" + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "deleteUser", startTime, System.currentTimeMillis());
            
            return Result.fail("删除用户失败: " + e.getMessage());
        }
    }
    
    /**
     * 更新用户状态
     * @param id 用户ID
     * @param params 状态参数
     * @return 更新结果
     */
    @PutMapping("/{id}/status")
    public Result<String> updateUserStatus(@PathVariable Long id, @RequestBody Map<String, Integer> params) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_ATTEMPT", "system", "更新用户状态尝试: 用户ID=" + id);
        LogUtils.logSecurityEvent(logger, "USER_STATUS_UPDATE_REQUEST", "接收到更新用户状态请求", "用户ID: " + id + ", 状态参数: " + params);
        LogUtils.debug(logger, () -> "开始更新用户状态: 用户ID=" + id + ", 状态参数=" + params);
        
        try {
            // 验证参数
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "更新用户状态失败: 用户ID不能为空或无效");
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 用户ID不能为空或无效");
                return Result.fail("用户ID不能为空或无效");
            }
            
            if (params == null || params.isEmpty()) {
                LogUtils.warn(logger, "更新用户状态失败: 状态参数不能为空");
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 状态参数不能为空");
                return Result.fail("状态参数不能为空");
            }
            
            Integer status = params.get("status");
            if (status == null) {
                LogUtils.warn(logger, "更新用户状态失败: 状态值不能为空");
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 状态值不能为空");
                return Result.fail("状态参数不能为空");
            }
            
            // 检查用户是否存在
            User user = userService.getById(id);
            if (user == null) {
                LogUtils.warn(logger, "更新用户状态失败: 用户不存在，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 用户不存在，用户ID=" + id);
                return Result.fail("用户不存在");
            }
            
            // 不允许禁用管理员用户
            if ("ADMIN".equals(user.getRole()) && status == 0) {
                LogUtils.warn(logger, "更新用户状态失败: 管理员用户不允许禁用，用户名={}", user.getUsername());
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 管理员用户不允许禁用，用户名=" + user.getUsername());
                LogUtils.logSecurityEvent(logger, "USER_DISABLE_ATTEMPTED_ADMIN", "尝试禁用管理员用户被拒绝", "用户ID: " + id + ", 用户名: " + user.getUsername());
                return Result.fail("管理员用户不允许禁用");
            }
            
            LogUtils.debug(logger, () -> "参数校验通过，开始更新用户状态: 用户ID=" + id + ", 目标状态=" + status + ", 用户名=" + user.getUsername() + ", 当前状态=" + user.getStatus());
            
            User updateUser = new User();
            updateUser.setId(id);
            updateUser.setStatus(status);
            
            boolean success = userService.updateById(updateUser);
            if (success) {
                String statusText = status == 1 ? "启用" : "禁用";
                LogUtils.info(logger, () -> "更新用户状态成功: 用户ID=" + id + ", 用户名=" + user.getUsername() + ", 新状态=" + status + "(" + statusText + ")");
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_SUCCESS", "system", "更新用户状态成功: 用户ID=" + id + ", 用户名=" + user.getUsername() + ", 新状态=" + status);
                LogUtils.logSecurityEvent(logger, "USER_STATUS_UPDATED", "用户状态更新成功", "用户ID: " + id + ", 用户名: " + user.getUsername() + ", 新状态: " + status + "(" + statusText + ")");
                LogUtils.logPerformance(logger, "updateUserStatus", startTime, System.currentTimeMillis());
                
                return Result.success("更新用户状态成功");
            } else {
                LogUtils.warn(logger, "更新用户状态失败: 用户ID={}, 目标状态={}", id, status);
                LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 用户ID=" + id + ", 目标状态=" + status);
                LogUtils.logPerformance(logger, "updateUserStatus", startTime, System.currentTimeMillis());
                
                return Result.fail("更新用户状态失败");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "更新用户状态失败: 用户ID=" + id + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_UPDATE_STATUS_FAILED", "system", "更新用户状态失败: 用户ID=" + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "updateUserStatus", startTime, System.currentTimeMillis());
            
            return Result.fail("更新用户状态失败: " + e.getMessage());
        }
    }
    
    /**
     * 重置用户密码
     * @param id 用户ID
     * @return 重置结果
     */
    @PutMapping("/{id}/reset-password")
    public Result<String> resetPassword(@PathVariable Long id) {
        long startTime = System.currentTimeMillis();
        LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_ATTEMPT", "system", "重置用户密码尝试: 用户ID=" + id);
        LogUtils.logSecurityEvent(logger, "USER_PASSWORD_RESET_REQUEST", "接收到重置用户密码请求", "用户ID: " + id);
        LogUtils.debug(logger, () -> "开始重置用户密码: 用户ID=" + id);
        
        try {
            // 验证参数
            if (id == null || id <= 0) {
                LogUtils.warn(logger, "重置用户密码失败: 用户ID不能为空或无效");
                LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_FAILED", "system", "重置用户密码失败: 用户ID不能为空或无效");
                return Result.fail("用户ID不能为空或无效");
            }
            
            // 检查用户是否存在
            User user = userService.getById(id);
            if (user == null) {
                LogUtils.warn(logger, "重置用户密码失败: 用户不存在，用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_FAILED", "system", "重置用户密码失败: 用户不存在，用户ID=" + id);
                return Result.fail("用户不存在");
            }
            
            LogUtils.debug(logger, () -> "参数校验通过，开始重置用户密码: 用户ID=" + id + ", 用户名=" + user.getUsername());
            
            // 这里应该实现密码重置逻辑，例如重置为默认密码并加密
            // 简化处理，实际应该调用userService的重置密码方法
            boolean success = true; // 假设重置成功
            
            if (success) {
                LogUtils.info(logger, () -> "重置用户密码成功: 用户ID=" + id + ", 用户名=" + user.getUsername());
                LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_SUCCESS", "system", "重置用户密码成功: 用户ID=" + id + ", 用户名=" + user.getUsername());
                LogUtils.logSecurityEvent(logger, "USER_PASSWORD_RESET", "用户密码重置成功", "用户ID: " + id + ", 用户名: " + user.getUsername());
                LogUtils.logPerformance(logger, "resetPassword", startTime, System.currentTimeMillis());
                
                return Result.success("密码重置成功");
            } else {
                LogUtils.warn(logger, "重置用户密码失败: 用户ID={}", id);
                LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_FAILED", "system", "重置用户密码失败: 用户ID=" + id);
                LogUtils.logPerformance(logger, "resetPassword", startTime, System.currentTimeMillis());
                
                return Result.fail("重置密码失败");
            }
        } catch (Exception e) {
            LogUtils.error(logger, "重置用户密码失败: 用户ID=" + id + ", 错误: " + e.getMessage());
            LogUtils.logBusiness(logger, "USER_RESET_PASSWORD_FAILED", "system", "重置用户密码失败: 用户ID=" + id + ", 错误: " + LogUtils.formatException(e));
            LogUtils.logPerformance(logger, "resetPassword", startTime, System.currentTimeMillis());
            
            return Result.fail("密码重置失败: " + e.getMessage());
        }
    }
}